﻿using blackhouse.AsyncMail.Business;
using blackhouse.AsyncMail.Business.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;

namespace blackhouse.AsyncMail.Web {
    public sealed class SendMail {

        #region Fields

        private ASyncMail mail;
        private string mailTitle;
        private int mailTemplateId;
        private object mailData;
        private Type mailDataType;
        private IASyncMailDataAccess dataLayer;

        #endregion

        #region Constructors

        public SendMail(string title, object mailData, int template) {
            this.mailTitle = title;
            this.mailData = mailData;
            this.mailDataType = this.mailData.GetType();
            this.mailTemplateId = template;
            this.dataLayer = new SqlASyncMailDataAccess(CacheTaskRunner.GetConnectionString());
        }

        #endregion

        #region Methods

        public void SendTo(string email) {
            this.SendTo(email, email);
        }

        public void SendTo(string email, string displayName) {
            this.SendTo(new MailAddress(email, displayName));
        }

        public void SendTo(MailAddress receiver) {
            this.mail = new ASyncMail(this.dataLayer, this.mailTemplateId, this.mailData, this.mailDataType, receiver, this.mailTitle);
            this.mail.Save();
        }

        #endregion

    }
}